---
title: 'last | Cypress Documentation'
description: Get the last DOM element within a set of DOM elements in Cypress.
sidebar_label: last
slug: /api/commands/last
---

<ProductHeading product="app" />

# last

Get the last DOM element within a set of DOM elements.

:::info

The querying behavior of this command matches exactly how
[`.last()`](http://api.jquery.com/last) works in jQuery.

:::

## Syntax

```javascript
.last()
.last(options)
```

### Usage

<Icon name="check-circle" color="green" /> **Correct Usage**

```javascript
cy.get('nav a').last() // Yield last link in nav
```

<Icon name="exclamation-triangle" color="red" /> **Incorrect Usage**

```javascript
cy.last() // Errors, cannot be chained off 'cy'
cy.getCookies().last() // Errors, 'getCookies' does not yield DOM element
```

### Arguments

<Icon name="angle-right" /> **options _(Object)_**

Pass in an options object to change the default behavior of `.last()`.

| Option    | Default                                                           | Description                                                                         |
| --------- | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| `log`     | `true`                                                            | Displays the command in the [Command log](/app/core-concepts/open-mode#Command-Log) |
| `timeout` | [`defaultCommandTimeout`](/app/references/configuration#Timeouts) | Time to wait for `.last()` to resolve before [timing out](#Timeouts)                |

<HeaderYields />

- `.last()` yields the new DOM element it found.
- `.last()` is a query, and it is _safe_ to chain further commands.

## Examples

### No Args

#### Get the last list item in a list

```html
<ul>
  <li class="one">Knick knack on my thumb</li>
  <li class="two">Knick knack on my shoe</li>
  <li class="three">Knick knack on my knee</li>
  <li class="four">Knick knack on my door</li>
</ul>
```

```javascript
// yields <li class="four">Knick knack on my door</li>
cy.get('li').last()
```

## Rules

<HeaderRequirements />

- `.last()` requires being chained off a command that yields DOM element(s).

<HeaderAssertions />

- `.last()` will automatically [retry](/app/core-concepts/retry-ability)
  until the element(s)
  [exist in the DOM](/app/core-concepts/introduction-to-cypress#Implicit-Assertions).
- `.last()` will automatically [retry](/app/core-concepts/retry-ability)
  until all chained assertions have passed.

<HeaderTimeouts />

- `.last()` can time out waiting for the element(s) to
  [exist in the DOM](/app/core-concepts/introduction-to-cypress#Implicit-Assertions).
- `.last()` can time out waiting for assertions you've added to pass.

## Command Log

**_Find the last button in the form_**

```javascript
cy.get('form').find('button').last()
```

The commands above will display in the Command Log as:

<DocsImage
  src="/img/api/last/find-the-last-button-in-a-form.png"
  alt="Command Log for last"
/>

When clicking on `last` within the command log, the console outputs the
following:

<DocsImage
  src="/img/api/last/inspect-last-element-in-console.png"
  alt="Console Log for last"
/>

## See also

- [`.eq()`](/api/commands/eq)
- [`.first()`](/api/commands/first)
